import { forget, getcode, valiEmail, checkEamilSet, sendTelCode } from '@/api';
import lang from '@/lang';


export default {
  data() {
    return {
      email: '',
      emailReg: /^[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?$/,
      phoneReg: /^1\d{10}$/,
      emailErr: false,
      emailErrText: '',
      time: 60,
      code: '',
      codeErr: false,
      codeErrText: '',
      password: '',
      passwordReg: /^(?=[0-9a-zA-Z!@#$%^&*]*[a-zA-Z])[0-9a-zA-Z!@#$%^&*]{6,32}$/, // 或 /^(?!^[0-9a-zA-Z!@#$%^&*]*$)[0-9a-zA-Z!@#$%^&*]{6,32}$/
      passwordErr: false,
      passwordErrText: '',
      rpwd: '',
      rpwdErr: false,
      rpwdErrText: '',
      loading: false,
      show: 1,
      getcode: {
        v_code_btn: true,
        codeStr: this.$t('btn.sendcode.getcode'),
        time: 60,
        timer: null,
      },
    };
  },
  beforeDestroy() {
    clearInterval(this.getcode.timer);
  },
  watch: {
    email(v) {
      this.emailErrText = v ? lang.t('login.email_input_sure') : lang.t('forget.email_info');
      this.emailErr = !this.emailReg.test(v) && !this.phoneReg.test(v);
    },
    code(v) {
      this.code = v.toString().replace(/\D/g, '');
      this.codeErrText = this.code
        ? lang.t('forget.email_sure') : lang.t('forget.email_code_info');
      this.codeErr = !this.code;
    },
    password(v) {
      this.passwordErrText = v
        ? lang.t('forget.pwd_info')
        : lang.t('login.pwd_input');
      this.passwordErr = !this.passwordReg.test(v);
      this.rpwdErrText = this.rpwd ? lang.t('forget.pwd_newpwd') : lang.t('forget.pwd_input_sure');
      this.rpwdErr = this.rpwd && this.rpwd !== this.password;
    },
    rpwd(v) {
      this.rpwdErrText = v ? lang.t('forget.pwd_newpwd') : lang.t('forget.again_pwd_info');
      this.rpwdErr = v !== this.password;
    },
  },
  methods: {
    countdown() {
      // const timer = window.setInterval(() => {
      //   if (this.time === 1) {
      //     this.time = 60;
      //     window.clearInterval(timer);
      //     return;
      //   }
      //   this.time -= 1;
      // }, 1000);
      this.getcode.v_code_btn = false;
      this.getcode.codeStr = `${this.$t('btn.sendcode.resend')}(60s)`;
      clearInterval(this.getcode.timer);
      this.getcode.timer = setInterval(() => {
        if (this.getcode.time > 0) {
          this.getcode.time -= 1;
          this.getcode.codeStr = `${this.$t('btn.sendcode.resend')}(${this.getcode.time}s)`;
        } else {
          clearInterval(this.getcode.timer);
          this.getcode.time = 60;
          this.getcode.codeStr = this.$t('btn.sendcode.resend');
          this.getcode.v_code_btn = true;
        }
      }, 1000);
    },
    sendCode() {
      if (this.getcode.time !== 60) {
        return;
      }
      if (!this.email) {
        this.emailErrText = lang.t('login.email_input');
        this.emailErr = true;
        return;
      }
      if (this.emailErr) {
        return;
      }
      checkEamilSet({
        email: this.email,
      }).then((response) => {
        if (response.data.status !== 1) {
          this.emailErrText = response.data.msg;
          this.emailErr = true;
        } else {
          this.time -= 1;
          this.countdown();
          if (this.emailReg.test(this.email)) {
            getcode({
              email: this.email,
              type: 'forget',
            }).then((res) => {
              if (res.data.status === 1) {
                this.$notify({
                  message: this.$ApiMessages(res.data.errcode, { email: this.email }),
                  type: 'success',
                  duration: 2000,
                  showClose: false,
                });
              } else {
                this.$message({
                  message: lang.t(`api.${res.data.errcode}`),
                  type: 'warning',
                });
              }
            }).catch(() => {
              this.$message.error(this.$t('error.catchError'));
            });
          } else {
            sendTelCode({
              tel: this.email,
              type: 'forget',
            }).then((res) => {
              if (res.data.status === '1') {
                this.$notify({
                  message: this.$t('set.send_phone_code'),
                  type: 'success',
                  duration: 2000,
                  showClose: false,
                });
              } else {
                this.$message({
                  message: this.$t(`api.${res.data.errcode}`),
                  type: 'warning',
                });
              }
            }).catch(() => {
              this.$message.error(this.$t('error.catchError'));
            });
          }
        }
      });
    },
    next() {
      if (this.loading) {
        return;
      }
      if (!this.email) {
        this.emailErrText = lang.t('login.email_input');
        this.emailErr = true;
        return;
      }
      if (this.emailErr) {
        return;
      }
      if (!this.code) {
        this.codeErrText = lang.t('forget.email_code_info');
        this.codeErr = true;
        return;
      }
      if (this.code.toString().length !== 6) {
        this.codeErrText = lang.t('forget.email_sure');
        this.codeErr = true;
        return;
      }
      this.loading = true;

      // 请求
      valiEmail({
        email: this.email,
        code: this.code,
      }).then((response) => {
        if (response.data.status === 1) {
          this.show = 2;
          this.$notify({
            message: this.$t('api.6047'),
            type: 'success',
            duration: 2000,
            showClose: false,
          });
        } else {
          this.$message({
            message: lang.t(`api.${response.data.errcode}`),
            type: 'warning',
          });
        }
        this.loading = false;
      }).catch(() => {
        this.$message.error(this.$t('error.catchError'));
        this.loading = false;
      });
    },
    forgetPwd() {
      if (this.loading) {
        return;
      }
      if (!this.password) {
        this.passwordErrText = lang.t('login.pwd_input');
        this.passwordErr = true;
        return;
      }
      if (this.passwordErr) {
        return;
      }
      if (!this.rpwd) {
        this.rpwdErrText = lang.t('forget.again_pwd_info');
        this.rpwdErr = true;
        return;
      }
      if (this.rpwdErr) {
        return;
      }
      this.loading = true;
      // 请求

      forget({
        email: this.email,
        password: this.password,
        rpwd: this.rpwd,
        code: this.code,
      }).then((response) => {
        if (response.data.status === 1) {
          this.show = 3;
          this.$notify({
            message: lang.t('forget.success_to'),
            type: 'success',
            duration: 1000,
            showClose: false,
          });
          // 清空cooice

          window.setTimeout(() => {
            this.$router.push('/login');
          }, 3000);
        } else if (response.data.status === 0) {
          this.$message({
            message: lang.t(`api.${response.data.errcode}`),
            type: 'warning',
          });
        }
        this.loading = false;
      }).catch(() => {
        this.loading = false;
        this.$message.error(this.$t('error.catchError'));
      });
    },
  },
};
